<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


	class LOGIN extends CI_Controller {

		public function __construct()
		{
			parent::__construct();

			$this->load->helper('url');
			$this->load->library('session');
					
			// $this->checkLoginWithCookie();


		}

		public function index(){

			$userData = $this->session->userdata('userData');

			if($userData){
				$this->loginWithCookie();
			}else{
				$this->load->view('admin/extra_login.html');
			}

			
		}

		
		public function checkLogin(){

			$login_name = $this->input->post('login_name');
			$password = md5($this->input->post('password'));

			$this->load->model('user_admin_model');
			$user_index = $this->user_admin_model->selectCode(array('login_name' => $login_name , 'password' => $password),'index');

			if($user_index){
				$userData = array('username' => $login_name, 'password' => $password);
				$this->session->set_userdata('userData',$userData);
			}else{
				echo "<script>alert('登录失败!');history.go(-1)</script>";
				return false;
			}

			if($user_index['index'] == '1'){
				
				// $url = 'http://localhost/cal/admin/user_admin/highest_admin';
				// echo "<script>window.location.href='".$url."';</script>";
				
				 redirect($config['base_url'].'admin/user_admin/highest_admin');

			}else if ($user_index['index'] == '1') {

		
				 redirect($config['base_url'].'admin/user_admin/highest_admin');
			}

		}

		public function loginWithCookie(){
			$userData = $this->session->userdata('userData');
			$username = $userData['username'];
			$password = $userData['password'];
			$this->load->model('user_admin_model');
			$user_index = $this->user_admin_model->selectCode(array('login_name' => $username , 'password' => $password),'index');

			if(!$user_index){
							
				$this->session->unset_userdata('userData');
				$this->index();
				return false;
			}
			if($user_index['index'] == '1'){
					
				// $url = 'http://localhost/cal/admin/user_admin/highest_admin';
				// echo "<script>window.location.href='".$url."';</script>";
				 // redirect($config['base_url'].'admin/user_admin/highest_admin');
				redirect('admin/user_admin/highest_admin');

			}else if ($user_index['index'] == '2') {

				 redirect('admin/user_admin/highest_admin');
			}
			
		}


	}

?>